package com.pkk.cloud.support.gataway.spring.config;

import static com.pkk.cloud.support.gataway.spring.config.custom.CustomHostAddrKeyResolver.KEY_RESOLVER_HOST;
import static com.pkk.cloud.support.gataway.spring.config.custom.CustomRequestPathKeyResolver.KEY_RESOLVER_REQUEST_URL;

import com.pkk.cloud.support.gataway.spring.config.custom.CustomHostAddrKeyResolver;
import com.pkk.cloud.support.gataway.spring.config.custom.CustomRequestPathKeyResolver;
import com.pkk.cloud.support.gataway.spring.filter.CustomGlobalFilter;
import com.pkk.cloud.support.gataway.spring.filter.RateCheckGatewayFilterFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * @description: 过滤器配置类
 * @author: peikunkun
 * @create: 2019-08-23 11:23
 **/
@Slf4j
@Configuration
public class GatewayFilterConfig {


  /**
   * @Description:接口限流配置
   * @Param: [defaultRateLimiter, defaultKeyResolver]
   * @return: com.pkk.cloud.support.gataway.filter.RateCheckGatewayFilterFactory
   * @Author: peikunkun
   * @Date: 2019/11/9 0009 上午 11:50
   */
  @Bean
  public RateCheckGatewayFilterFactory rateCheckGatewayFilterFactory(RateLimiter defaultRateLimiter,
      @Qualifier("apiKeyResolver") KeyResolver defaultKeyResolver) {
    return new RateCheckGatewayFilterFactory(defaultRateLimiter, defaultKeyResolver);
  }


  /**
   * @Description: 全局过滤配置器
   * @Param: []
   * @return: com.pkk.cloud.support.gataway.filter.CustomGlobalFilter
   * @Author: peikunkun
   * @Date: 2019/8/23 0023 上午 11:28
   */
  @Bean
  public CustomGlobalFilter customGlobalFilter() {
    return new CustomGlobalFilter();
  }


  /**
   * @Description: 自定义请求路径
   * @Param: []
   * @return: com.pkk.cloud.support.gataway.config.custom.CustomRequestPathKeyResolver
   * @Author: peikunkun
   * @Date: 2019/8/23 0023 下午 4:51
   */
  @Bean(KEY_RESOLVER_REQUEST_URL)
  public CustomRequestPathKeyResolver customRequestPathKeyResolver() {
    log.info(this.getClass().getSimpleName() + " is init");
    return new CustomRequestPathKeyResolver();
  }


  /**
   * @Description:配置依据主机的方式
   * @Param: []
   * @return: com.pkk.cloud.support.gataway.config.custom.CustomHostAddrKeyResolver
   * @Author: peikunkun
   * @Date: 2019/8/23 0023 下午 4:59
   */
  @Bean(KEY_RESOLVER_HOST)
  public CustomHostAddrKeyResolver hostAddrKeyResolver() {
    log.info(this.getClass().getSimpleName() + " is init");
    return new CustomHostAddrKeyResolver();
  }


  /**
   * 接口限流操作
   *
   * @return
   */
  @Bean(name = "apiKeyResolver")
  public KeyResolver apiKeyResolver() {
    log.info("根据路径初始化配置：" + this.getClass().getSimpleName());
    //根据api接口来限流
    return exchange -> Mono.just(exchange.getRequest().getPath().value());
  }

  /**
   * ip限流操作
   *
   * @return
   */
  /*@Bean("ipKeyResolver")
  public KeyResolver ipKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
  }*/

  /**
   * 用户限流
   * 使用这种方式限流，请求路径中必须携带userId参数。
   * @return
   */
//    @Bean
//    KeyResolver userKeyResolver() {
//        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
//    }


}
